package com.shop.cereshop.app.controller.shop;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.domain.shop.CereShopCommentVO;
import com.shop.cereshop.app.domain.shop.ShopVO;
import com.shop.cereshop.app.page.banner.ShopBanner;
import com.shop.cereshop.app.page.settlement.ShopInfo;
import com.shop.cereshop.app.page.shop.Shop;
import com.shop.cereshop.app.page.shop.ShopClassify;
import com.shop.cereshop.app.page.shop.ShopIndex;
import com.shop.cereshop.app.param.shop.SearchShopParam;
import com.shop.cereshop.app.param.shop.*;
import com.shop.cereshop.app.service.business.CereBusinessBuyerUserService;
import com.shop.cereshop.app.service.buyer.CereBuyerSearchService;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.app.service.shop.CerePlatformShopService;
import com.shop.cereshop.app.service.shop.PlatformShopV2Service;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.business.CereBusinessBuyerUser;
import com.shop.cereshop.commons.domain.buyer.CereBuyerSearch;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.domain.shop.ShopVo;
import com.shop.cereshop.commons.enums.SearchTypeEnum;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("shops")
@Slf4j(topic = "ShopNewController")
@Api(value = "店铺新模块", tags = "店铺新模块")
public class ShopNewController {

    @Resource
    private CerePlatformShopService cerePlatformShopservice;
    @Resource
    private CereBuyerUserService cereBuyerUserService;
    @Resource
    private CereBusinessBuyerUserService cereBusinessBuyerUserService;
    @Resource
    private CereBuyerSearchService cereBuyerSearchService;
    @Resource
    private PlatformShopV2Service platformShopV2Service;


    @ApiOperation(value = "商品搜索查询")
    @GetMapping("getShopProducts")
    public Result<Shop> getShopProducts(ShopParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        Shop shop = cerePlatformShopservice.getShopProducts(param, user);
        return new Result(shop, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "新店铺搜索")
    @GetMapping("/getShops")
    public Result<Page<CereShopCommentVO>> getShops(ShopParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        Page page = cerePlatformShopservice.getShops(param, user);

        return new Result(page, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "查询周边店铺")
    @GetMapping("getShopList")
    public Result<IPage<ShopInfo>> getShopList(ShopQueryParam param) {
        IPage<ShopInfo> page = cerePlatformShopservice.getShopInfoListByParam(param);
        return new Result(page, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "查询店铺分类")
    @GetMapping("getShopClassify")
    public Result<List<ShopClassify>> getShopClassify(ShopParam param) throws CoBusinessException {
        List<ShopClassify> list = cerePlatformShopservice.getShopClassify(param);
        return new Result(list, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "查询店铺banner")
    @GetMapping("getShopBanner")
    public Result<List<ShopBanner>> getShopBanner(ShopParam param) throws CoBusinessException {
        List<ShopBanner> list = cerePlatformShopservice.getShopBanner(param.getShopId());
        return new Result(list, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "店铺首页查询")
    @GetMapping("getIndex")
    public Result<ShopIndex> getIndex(ShopParam param, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        ShopIndex index = cerePlatformShopservice.getIndex(param, user);
        return new Result(index, CoReturnFormat.SUCCESS);
    }


    @ApiOperation(value = "获取分享图片")
    @GetMapping("getSharePic")
    public Result<String> getSharePic(ShopPosterParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        String sharePicUrl = cerePlatformShopservice.getSharePic(param, user);
        Result<String> result = new Result();
        result.setCode(CoReturnFormat.SUCCESS);
        result.setData(sharePicUrl);
        return result;
    }

    @ApiOperation(value = "添加客户访问商家的记录")
    @PostMapping("addBusinessBuyerUser")
    public Result<Boolean> addBusinessBuyerUser(@RequestBody CereBusinessBuyerUser cereBusinessBuyerUser, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
            if (user != null && user.getBuyerUserId() != null) {
                cereBusinessBuyerUser.setBuyerUserId(user.getBuyerUserId());
                cereBusinessBuyerUserService.addBusinessBuyerUser(cereBusinessBuyerUser);
            }
        }
        return new Result(true, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "查询店铺热榜数据")
    @GetMapping("shopHot")
    public Result<PageInfo<ShopVo>> shopHot(PageParam param) {
        PageInfo<ShopVo> page = cerePlatformShopservice.shopHot(param);
        return new Result<>(page, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "获取店铺详情信息")
    @GetMapping("getShopInfo/{shopId}")
    public Result<ShopInfo> getShopInfo(@NotNull @PathVariable("shopId") Long shopId, HttpServletRequest request) throws CoBusinessException, ParseException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        ShopInfo shopInfo = cerePlatformShopservice.getShopInfo(shopId, user);
        return new Result<>(shopInfo, CoReturnFormat.SUCCESS);
    }


    @ApiOperation("推荐店铺列表")
    @GetMapping("/suggest")
    public Result<Page<ShopVO>> getSuggestShopList(@NotNull SuggestShopParam param) {
        return platformShopV2Service.getSuggestShopList(param);
    }

    @ApiOperation("搜索店铺列表（新版）")
    @GetMapping("/search")
    public Result<Page<ShopVO>> searchShopList(HttpServletRequest request, @NotNull SearchShopParam param) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user != null) {
            cereBuyerSearchService.save(user.getBuyerUserId(), param.getKeyword(), SearchTypeEnum.SHOP.getCode());
        }
        return platformShopV2Service.searchShopList(param);
    }

    @ApiOperation(value = "查询店铺热榜（新版）")
    @GetMapping("hotList")
    public Result<List<ShopVO>> shopHotList(
            @RequestParam(value = "cityCode") @NotNull(message = "cityCode不能为空") @ApiParam(value = "城市编码") String cityCode) {
        return platformShopV2Service.shopHotList(cityCode);
    }

    @ApiOperation(value = "获取店铺详情信息（新版）")
    @GetMapping("/detail")
    public Result<ShopVO> getShopDetail (
            @RequestParam(value = "shopId") @NotNull(message = "shopId不能为空") @ApiParam(value = "店铺ID", required = true) Long shopId,
            @RequestParam(value = "shopType") @NotNull(message = "shopType不能为空") @ApiParam(value = "店铺类型", required = true) Integer shopType,
            @RequestParam(value = "cityCode", defaultValue = "") @ApiParam(value = "城市编码") String cityCode,
            HttpServletRequest request) throws Exception {
        Long buyerUserId = null;
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user != null) {
            buyerUserId = user.getBuyerUserId();
        }
        ShopVO shopVO = platformShopV2Service.getShopDetail(shopId, shopType, buyerUserId, cityCode);
        return new Result<>(shopVO, CoReturnFormat.SUCCESS);
    }

}
